const connection = require('../app/database');

class IndivService {

  // 创建一条介绍
  async create(userId, indivKey, indivValue, mini) {
    try {
      const statement = `INSERT INTO indiv (user_id, indiv_key, indiv_value, mini)  VALUES (?, ?, ?, ?)`;
      const result = await connection.execute(statement, [userId, indivKey, indivValue, mini]);
      return result[0]
    } catch (error) {
      console.log(error);
    }
  }

  // 删除一条介绍
  async remove(userId, indivId) {
    try {
      const statement = `DELETE FROM indiv WHERE id = ? AND user_id = ?`;
      const result = await connection.execute(statement, [indivId, userId]);
      return result[0]
    } catch (error) {
      console.log(error);
    }
  }

  // 修改一条介绍
  async update(userId, indivId, indivKey, indivValue, mini) {
    try {
      const statement = `UPDATE indiv SET indiv_key = ?, indiv_value = ?, mini = ? WHERE id = ? AND user_id = ?`;
      const result = await connection.execute(statement, [indivKey, indivValue, mini, indivId, userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }

  // 获取简介列表
  async list(userId) {
    try {
      const statement = `SELECT id 'key', indiv_key indivKey, indiv_value indivValue, mini, createAt createTime FROM indiv WHERE user_id = ?`;
      const result = await connection.execute(statement, [userId]);
      return result[0];
    } catch (error) {
      console.log(error);
    }
  }


}

module.exports = new IndivService();
